<?php

	namespace Phptricks\app\controllers\pages;

/*
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 */
	/**
	 * Description of newPHPClass
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\app\controllers\base\base as base;
	use Phptricks\app\models\pages\pages as pages_model;

	class pages extends base{

		public $pages_model = null;

		public function __construct(){
			parent::__construct();
			$this->pages_model = pages_model::getinstance();
		}

		public function _initialize(){
			
		}

		public function view($page_name = ''){
			if (empty($page_name)) {
				$this->display_message('danger', $this->lang['error_404'], 'index/', 5);
			}
			$array_where = array(
				'page_name' => \str_replace('_', ' ', $page_name)
			);
			$data = $this->pages_model->getpages($array_where);
			if (empty($data[0]['page_name'])) {
				$this->display_message('danger', $this->lang['error_404'], 'index/', 5);
			}
			$this->view->assign('data', $data[0]);
			$this->view->display('pages.tpl');
		}

		public function admin_index(){
			$data = $this->pages_model->getpages();
			$this->view->assign('data', $data);
			$this->view->display('pages.tpl');
		}

		public function admin_view(){
			$this->admin_index();
		}

		public function admin_add(){
			$this->view->display('pages.tpl');
		}

		public function admin_addnew(){
			$this->form_validation->post(array(
				'page_name' => 'isnull',
				'page_order' => 'isnull|digit',
				'page_text' => 'isnull'
			));
			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), 'admin/pages/add/', 5);
			}
			$this->pages_model->addpages($this->form_validation->_array_data);
			$this->display_message('success', $this->lang['success_add_data'], 'admin/pages/view/', 3);
		}

		public function admin_edit($id = 0){
			if (empty($id)) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/pages/view/', 5);
			}
			$id = \intval($id);
			$where_array = array(
				'page_id' => $id
			);
			$data = $this->pages_model->getpages($where_array);
			$this->view->assign('data', $data[0]);
			$this->view->display('pages.tpl');
		}

		public function admin_update(){
			$this->form_validation->post(array(
				'page_id' => 'isnull|digit',
				'page_name' => 'isnull',
				'page_order' => 'isnull|digit',
				'page_text' => 'isnull'
			));

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$url = 'admin/pages/edit/' . $this->form_validation->_array_data['page_id'];
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), $url, 5);
			}
			$id = $this->form_validation->_array_data['page_id'];
			$array_where = array(
				'page_id' => $id
			);
			$this->form_validation->unset_var('page_id', 'post');
			$this->pages_model->updatepages($this->form_validation->_array_data, $array_where);
			$this->display_message('success', $this->lang['success_edit_data'], 'admin/pages/view/', 3);
		}

		public function admin_delete($id){
			if (!empty($id) && isset($id)) {
				if (\is_array($id)) {
					$id = \array_filter($id, function(&$id_val) {
						echo $id_val . "<br>";
						return \intval($id_val);
					});
				} else {
					$id = \intval($id);
				}
				$where_array = array(
					'page_id' => $id
				);
				$this->pages_model->deletepages($where_array);
				$this->display_message('success', $this->lang['success_delete_data'], 'admin/pages/view/', 3);
			}
		}

		public function admin_action(){
			if ($this->form_validation->is_set('row_id') == false) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/pages/view/', 5);
			}
			$id = $this->form_validation->_post['row_id'];
			if (\is_array($id)) {
				$id = \array_filter($id, function(&$id_val) {
					return \intval($id_val);
				});
			} else {
				$id = \intval($id);
			}

			if (!empty($id)) {
				$where_array = array(
					'page_id' => $id
				);
				try {
					$this->pages_model->deletepages($where_array);
				} catch (\Exception $exc) {
					throw new Exc($exc->getMessage());
				}
				$this->display_message('success', $this->lang['success_delete_data'], 'admin/pages/view/', 3);
			}
		}

	}
	